package com.bt.springboot.web.controller;

import com.bt.springboot.common.util.SocketUtil;
import com.bt.springboot.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author zkx
 * @Date 2023/5/6 15:11
 */
@RestController
public class SocketController {

	public static final String SEND_TYPE_ALL = "ALL";
	public static final String SEND_TYPE_ALONE = "ALONE";

	@Autowired
	private SocketUtil socketUtil;

	@PostMapping("/testSendMsg")
	public String testSendMsg(@RequestBody Message message) {
		HashMap<String, Object> map = new HashMap<>();
		map.put("msg", message.getContent());
		// 群发
		if (SEND_TYPE_ALL.equals(message.getType())) {
			socketUtil.sendToAll(map, message.getChannel());
			return "success";
		}
		// 指定单人
		if (SEND_TYPE_ALONE.equals(message.getType())) {
			socketUtil.sendToOne(message.getTo(), map, message.getChannel());
			return "success";
		}
		return "fail";
	}
}
